package com.simtop.util;

import ws.schild.jave.*;

import java.io.File;
import java.util.Date;

public class AviToMp4Util {
    public static void convert(File source, File target) {
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame"); //音频编码格式
        audio.setBitRate(new Integer(800000));
        audio.setChannels(new Integer(1));
        //audio.setSamplingRate(new Integer(22050));
        VideoAttributes video = new VideoAttributes();
        video.setCodec("libx264");//视频编码格式
        video.setBitRate(new Integer(3200000));
        video.setFrameRate(new Integer(15));//数字设置小了，视频会卡顿
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp4");
        attrs.setAudioAttributes(audio);
        attrs.setVideoAttributes(video);
        Encoder encoder = new Encoder();
        MultimediaObject multimediaObject = new MultimediaObject(source);
        try {
            System.out.println("avi转MP4 --- 转换开始:" + new Date());
            encoder.encode(multimediaObject, target, attrs);
            System.out.println("avi转MP4 --- 转换结束:" + new Date());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InputFormatException e) {
            e.printStackTrace();
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
}

